<template>
    <view style="height: 80%; overflow: hidden;">
        <view>
            欢迎！更多功能待完善中...<br><br>
            👉点击左侧<strong style="color:red">Rancher CD</strong>菜单栏，使用更多功能。
        </view>
        
        <view style="height: 320rpx;">
            <uni-section title="新增或重新部署" type="circle" title-font-size="30px" style="margin-top: 25px;"></uni-section>
            <uni-table ref="table" :loading="false" border stripe emptyText="暂无更多数据">
                <uni-tr>
                    <uni-th width="130">命名空间</uni-th>
                    <uni-th width="130">工作负载</uni-th>
                    <uni-th width="130">（可选）docker镜像版本</uni-th>
                    <!-- <uni-th width="110">是否重新部署</uni-th> -->
                    <!-- <uni-th width="110">是否通知</uni-th> -->
                    <uni-th width="110">操作</uni-th>
                </uni-tr>
                <uni-tr>
                    <uni-td>
                        <uni-data-select :clear="true" v-model="workload.namespace" :localdata="workload.namespaceRange" />
                    </uni-td>
                    <uni-td>
                        <uni-easyinput v-model="workload.workloadName" :clearable="false" placeholder="请输入工作负载名" />
                    </uni-td>
                    <uni-td>
                        <uni-easyinput v-model="workload.imageTag" :clearable="false" placeholder="（可选）请输入docker镜像版本" />
                    </uni-td>
<!--                    <uni-td>
                        <switch @change="isRedeployChange($event.detail.value)" :checked="workload.isRedeploy" />
                    </uni-td>
                    <uni-td>
                        <switch @change="isNotifyChange($event.detail.value)" :checked="workload.isNotify" />
                    </uni-td> -->
                    <uni-td>
                        <button @click="cd(workload)" class="uni-button" size="mini">新增或重新部署工作负载</button>
                    </uni-td>
                </uni-tr>
            </uni-table>
        </view>

        <view style="height: 1200rpx; overflow: hidden;">
            <uni-section title="部署日志" type="circle" title-font-size="30px" style="margin-top: 25px;"></uni-section>
            <uni-table ref="workloadRedeployLogTable" :loading="loading" border stripe emptyText="暂无更多数据">
                <uni-tr>
                    <uni-th width="110">命名空间</uni-th>
                    <uni-th width="110">工作负载</uni-th>
                    <uni-th width="110">发布版本</uni-th>
                    <uni-th width="110">是否成功</uni-th>
                    <uni-th width="110">是否通知</uni-th>
                    <uni-th width="110">发布时间</uni-th>
                </uni-tr>
                <uni-tr v-for="(item, index) in tableData" :key="index">
                    <uni-td>{{ item.namespace }}</uni-td>
                    <uni-td>{{ item.workloadName }}</uni-td>
                    <uni-td>{{ item.redeployImageTag }}</uni-td>
                    <uni-td>是</uni-td>
                    <uni-td>否</uni-td>
                    <uni-td>
                        <uni-dateformat :date="item.createTime" format="yyyy-MM-dd hh:mm:ss"></uni-dateformat>
                    </uni-td>
                </uni-tr>
            </uni-table>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                workload: {
                    namespace: "",
                    namespaceRange: [{
                            value: "test1",
                            text: "test1"
                        },
                        {
                            value: "test2",
                            text: "test2"
                        }
                    ],
                    isRedeploy: false,
                    isNotify: false
                },
                searchVal: '',
                tableData: [],
                // 每页数据量
                pageSize: 10,
                pageSizeRange: [10, 20, 50, 200],
                // 当前页
                pageNum: 1,
                // 数据总量
                total: 0,
                loading: false,
                selectedIndexs: []
            }
        },
        onLoad() {
            this.getNamespace()
            this.getWorkloadRedeployLog(1)
        },
        methods: {
            // 获得命名空间
            getNamespace() {
                uni.request({
                    url: '/auth/v1/confRancher/listNamespace',
                    dataType: 'string',
                    success: (res) => {
                        let data = res.data.data;
                        for (var i = 0; i < data.length; i++) {
                            this.workload.namespaceRange[i] = {
                                "value": data[i],
                                "text": data[i]
                            }
                        }
                    }
                });
            },
            // 获取工作负载部署日志
            getWorkloadRedeployLog(pageNum, value = '') {
                this.loading = true
                this.pageNum = pageNum

                uni.request({
                    url: '/auth/v1/workloadRedeployLog/page',
                    dataType: 'string',
                    data: {
                        "pageNum": pageNum,
                        "pageSize": this.pageSize,
                        "orderBy": "id DESC"
                    },
                    success: (res) => {
                        let data = res.data.data;
                        this.pageNum = data.pageNum;
                        this.total = data.total;
                        this.tableData = [];
                        this.tableData = data.list;
                    }
                });
                this.loading = false
            },
            cd(workload) {
                if (workload.namespace == null || workload.namespace == "") {
                    uni.showToast({
                        title: "命名空间不能为空",
                        icon: 'error',
                        duration: 2000
                    });
                    return;
                } else if (workload.workloadName == null || workload.workloadName == ""){
                    uni.showToast({
                        title: "工作负载不能为空",
                        icon: 'error',
                        duration: 2000
                    });
                    return;
                }
                let requestParam = {
                    "namespace": workload.namespace,
                    "workloadName": workload.workloadName
                }
                if (workload.imageTag != null && workload.imageTag != "") {
                    requestParam.imageTag = workload.imageTag
                }
                
                this.loading = true
                uni.request({
                    method: "PUT",
                    url: '/devops/cd',
                    data: requestParam,
                    success: (res) => {
                        if (res.data.code == 200) {
                            uni.showToast({
                                title: '部署成功'
                            })
                        }
                    }
                });
                this.loading = false
            },
            deleteById(id, index) {
                this.loading = true
                uni.request({
                    method: "DELETE",
                    url: '/auth/v1/workloadRedeployLog/deleteById',
                    data: {
                        id: id
                    },
                    success: (res) => {
                        if (res.data.code == 200) {
                            uni.showToast({
                                title: '删除成功'
                            })
                        }
                    }
                });

                this.tableData.splice(index, 1)
                this.total = this.total - 1
                this.loading = false
                this.$refs.workloadRedeployLogTable.clearSelection()
            },
            // 批量删除
            deleteByIds() {
                this.loading = true
                let ids = [];
                let selectedItems = this.selectedItems();
                for (var i = 0; i < selectedItems.length; i++) {
                    ids[i] = selectedItems[i].id;
                }
                uni.request({
                    method: "DELETE",
                    url: '/auth/v1/workloadRedeployLog/deleteByIds',
                    data: {
                        ids: ids
                    },
                    success: (res) => {
                        if (res.data.code == 200) {
                            uni.showToast({
                                title: '删除成功'
                            })
                        }
                    }
                });

                this.$refs.workloadRedeployLogTable.clearSelection()
                this.selectedIndexs.length = 0
                setTimeout(() => {
                    this.getData(1)
                }, 200)
            },
            isRedeployChange(e) {
                this.workload.isRedeploy = e
            },
            isNotifyChange(e) {
                this.workload.isNotify = e
            }
        }
    }
</script>

<style lang="scss">
    .uni-group {
        display: flex;
        align-items: center;
    }
</style>
